/**
 * Writed by yura and mak
 */

package wz.legend.gui;


import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import wz.legend.ProxyServer;
import wz.legend.Status;
import wz.legend.util.JXTrayIcon;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Tray {
    private static Tray instance = new Tray();
    private JXTrayIcon trayIcon;
    JPopupMenu popupMenu;
    JMenu statusMenu;
    ButtonGroup statusGroup;

    public Tray() {
        if (!SystemTray.isSupported()) {
            return;
        }
        ImageIcon icon = new ImageIcon(Tray.class.getClassLoader().getResource(
                "images/DWarC2.png"));
        trayIcon = new JXTrayIcon(icon.getImage());
        final SystemTray tray = SystemTray.getSystemTray();
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            throw new RuntimeException(e);
        }

        popupMenu = new JPopupMenu();
        statusMenu = new JMenu("Status");
        statusGroup = new ButtonGroup();
        popupMenu.add(statusMenu);
        popupMenu.addSeparator();

        JMenuItem menuItem = new JMenuItem("Product`s costs");
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new ProductPanel().setVisible(true);
            }
        });
        popupMenu.add(menuItem);
        popupMenu.addSeparator();

        menuItem = new JMenuItem("Exit");
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        popupMenu.add(menuItem);

        addStatusMenuItem("Farming", Status.FARMING);
        addStatusMenuItem("Buying", Status.BUYING);
        addStatusMenuItem("Instance leader", Status.INSTANCE_LEADER);
        addStatusMenuItem("Instance participant", Status.INSTANCE_PARTICIPANT);
        addStatusMenuItem("Trauma", Status.TRAUMA);
        addStatusMenuItem("Fighting", Status.FIGHTING);
        addStatusMenuItem("Walking", Status.WALKING);
        addStatusMenuItem("Inactive", Status.INACTIVE);
        
        popupMenu.addPopupMenuListener(new StatusMenuChanger());

        trayIcon.setJPopuMenu(popupMenu);
    }

    public static Tray getInstance() {
        return instance;
    }

    private void addStatusMenuItem(String name, Status status) {
        StatusMenuItem menuItem = new StatusMenuItem(name, status);
        menuItem.addItemListener(new StatusItemListener(menuItem));
        statusMenu.add(menuItem);
        statusGroup.add(menuItem);
    }
    
    public void showMessage(String message) {
		showMessage("", message, TrayIcon.MessageType.NONE);
	}

    public void showMessage(String title, String message) {
        trayIcon.displayMessage(title, message, TrayIcon.MessageType.NONE);
    }

    public void showMessage(String title, String message, TrayIcon.MessageType type) {
        trayIcon.displayMessage(title, message, type);
    }

    private class StatusMenuItem extends JRadioButtonMenuItem {
        private Status status;

        public StatusMenuItem(String name, Status status) {
            super(name);
            this.status = status;
        }

        public Status getStatus() {
            return status;
        }
    }

    private class StatusItemListener implements ItemListener {

        StatusMenuItem menuItem;

        public StatusItemListener(StatusMenuItem menuItem) {
            this.menuItem = menuItem;
        }

        public void itemStateChanged(ItemEvent e) {
            if (menuItem.isSelected())
                ProxyServer.getContext().setStatus(menuItem.getStatus());
        }
    }

    private class StatusMenuChanger implements PopupMenuListener {

        public void popupMenuCanceled(PopupMenuEvent e) {
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            for (int i = 0; i < statusMenu.getItemCount(); i++) {
                StatusMenuItem item = (StatusMenuItem) statusMenu.getItem(i);
                item.setSelected(ProxyServer.getContext().getStatus() == item
                        .getStatus());

            }
        }
    }
    
}
